package com.sktlab.bizconfmobile.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Application;

import com.sktlab.bizconfmobile.fragment.HomeFragment;
import com.sktlab.bizconfmobile.model.AccessNumber;
import com.sktlab.bizconfmobile.model.ConferenceAccount;
import com.sktlab.bizconfmobile.model.manager.AccountsManager;
import com.sktlab.bizconfmobile.model.manager.ContactManager;
import com.sktlab.bizconfmobile.util.Util;

public class AppClass extends Application {

	public static final String TAG = "AppClass";

	private static final int THEAD_POOL_SIZE = 5;
	
	//when  need background execute thread, we use this thread pool
	private ExecutorService service = null;
	
	//when launch the apk, check the network state
	private boolean isNetworkReady = false;
	
	private boolean isPassNumberVerify = false;
	//this is used to mark the user current select in the home page,such as start conference,
	//order conference or join conference
	private int currentTab = -1;
	
	//store the access number for worldwide
	private List<AccessNumber> accessNumberList;
	//this used to mark the access number user selected when build a account
	private String accessNumber;
	
	//This variable used to monitor the clear memory operation of the user,when clear memory,our app should restart
	//we add some object to it in welcomeActivity
	public static List<ConferenceAccount> signal;
	
	private static class InstanceHolder {

		private static AppClass instance = null;
	}
	
	/**
	 * get the signal instance of the application
	 * @return
	 */
	public static AppClass getInstance() {

		if (InstanceHolder.instance == null) {
			
			InstanceHolder.instance = new AppClass();		
			InstanceHolder.instance.init();
		}

		return InstanceHolder.instance;
	}

	public void onCreate() {
		super.onCreate();

		InstanceHolder.instance = this;
		
		init();
	}
	
	public void init() {

		// initialize service pool
		int cpuNums = Runtime.getRuntime().availableProcessors();
		setService(Executors.newFixedThreadPool(cpuNums * THEAD_POOL_SIZE));
		setNetworkReady(Util.isNetworkReadyForConf(this));

		currentTab = HomeFragment.TAB_START_CONF;
		accessNumber = "null";
		accessNumberList = new ArrayList<AccessNumber>();
		
		signal = new ArrayList<ConferenceAccount>();
	}
	
	public void reset(){
		
		currentTab = HomeFragment.TAB_START_CONF;
		accessNumber = "null";
		accessNumberList.clear();
		
		isPassNumberVerify = false;
		signal.clear();
		
		AccountsManager.getInstance().reset();
		ContactManager.getInstance().reset();
	}
	
	public ExecutorService getService() {
		return service;
	}

	public void setService(ExecutorService service) {
		this.service = service;
	}

	public boolean isNetworkReady() {
		return isNetworkReady;
	}

	public void setNetworkReady(boolean isNetworkReady) {
		this.isNetworkReady = isNetworkReady;
	}

	public int getCurrentTab() {
		return currentTab;
	}

	public void setCurrentTab(int currentTab) {
		this.currentTab = currentTab;
	}

	public List<AccessNumber> getAccessNumberList() {
		return accessNumberList;
	}
	
	public void addAccessNumber(AccessNumber an) {
		
		if (!Util.isEmpty(accessNumberList)) {
			
			accessNumberList.add(an);
		}
	}
	
	public void setAccessNumberList(List<AccessNumber> accessNumberList) {
		this.accessNumberList = accessNumberList;
	}

	public String getAccessNumber() {
		return accessNumber;
	}

	public void setAccessNumber(String accessNumber) {
		this.accessNumber = accessNumber;
	}

	public boolean isPassNumberVerify() {
		return isPassNumberVerify;
	}

	public void setPassNumberVerify(boolean isPassNumberVerify) {
		this.isPassNumberVerify = isPassNumberVerify;
	}
}
